package com.example.pokemon_chat_room.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.pokemon_chat_room.MainActivity;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.pojo.MyItemsDto;
import com.example.pokemon_chat_room.ui.dashboard.DashboardFragment;
import com.example.pokemon_chat_room.util.MyImageView;

import java.util.List;

public class MyItemBindAdapter extends RecyclerView.Adapter<MyItemBindAdapter.ViewHolder> {



    private List<MyItemsDto> myItemsList;
    private View view;
    private Context context;

    public MyItemBindAdapter(Context context, List<MyItemsDto> myItemsList) {
        this.context = context;
        this.myItemsList = myItemsList;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private MyImageView items_img;
        private TextView items_txt;
        private TextView items_count;
        public ViewHolder(View view) {
            super(view);
            // Define click listener for the ViewHolder's View

            items_img = view.findViewById(R.id.items_img);
            items_txt = view.findViewById(R.id.items_txt);
            items_count = view.findViewById(R.id.items_count);

        }

    }


    @NonNull
    @Override
    public MyItemBindAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_items_item_layout, parent, false);
        MyItemBindAdapter.ViewHolder holder = new MyItemBindAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyItemBindAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        int type = myItemsList.get(position).getType();

        holder.items_img.setImageURL("poke/items/ys/" + myItemsList.get(position).getImage());
        holder.items_txt.setText(myItemsList.get(position).getName());
        String itemsCount = String.valueOf(myItemsList.get(position).getItemsCount());

        holder.items_count.setText(itemsCount);

        holder.items_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyItemsDto myItemsDto = myItemsList.get(position);
                if (myItemsDto.getType() == 1 || myItemsDto.getItemsCount() == 0) {
                    sendMessageBroadcast(String.valueOf(0));
                    return;
                }
                int id = myItemsDto.getId();
                sendMessageBroadcast(String.valueOf(id));

            }
        });

    }

    @Override
    public int getItemCount() {
        return myItemsList.size();
    }

    private void sendMessageBroadcast(String message){
        if (!message.isEmpty()){
            Intent intent = new Intent();
            intent.setAction(ACTION_EXPIRED_MESSAGE);
            intent.putExtra(KEY_RECEIVED_DATA, message);
            context.sendBroadcast(intent);
        }
    }

    public static final String ACTION_EXPIRED_MESSAGE = "com.example.pokemon_chat_room.ACTION";
    public static final String KEY_RECEIVED_DATA = "catch";
}
